﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Util;
using System.Web.Mvc;

namespace MShop.Web.Components.ModelBinders
{
   

    public sealed class PayModelBinder:System.Web.Mvc.DefaultModelBinder
    {
        private readonly IPayPluginDao _payPluginDao;
        private Pay _Pay;

        //public PayModelBinder()
        //{
        //    _payPluginDao = IoC.Resolve<IPayPluginDao>();
        //}

        public PayModelBinder(IPayPluginDao payplugindao)
        {
            _payPluginDao = payplugindao;
        }
        public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
        {
            _Pay = (Pay)base.BindModel(controllerContext, bindingContext);
         
            return _Pay;
        }

        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            _Pay = (Pay)base.CreateModel(controllerContext, bindingContext, modelType);
            return _Pay;
        }

        protected override System.ComponentModel.PropertyDescriptorCollection GetModelProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var properties = base.GetModelProperties(controllerContext, bindingContext);
            return properties.Sort(new string[] { "PayType", "PayModel" });       
        }


        private object GetValue(ModelBindingContext bindingContext, string key, System.Reflection.PropertyInfo property)
        {
            ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(key);
            bindingContext.ModelState.SetModelValue(key, valueResult);
            return valueResult.ConvertTo(property.PropertyType);
        }

        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.PropertyType.IsInterface)
            {
                if (propertyDescriptor.PropertyType.Name == "IPayModel")
                {

                    IPayModel payModel = _payPluginDao.GetPayModel(int.Parse(_Pay.PayType));

                    System.Reflection.PropertyInfo[] propertyinfos = payModel.GetType().GetProperties();
                    foreach (System.Reflection.PropertyInfo prop in propertyinfos)
                    {
                        string key = string.Concat(propertyDescriptor.Name, ".", prop.Name);
                        object value = GetValue(bindingContext, key, prop);
                        prop.SetValue(payModel, value, null);
                    }
                    _Pay.SetPayModel(payModel);
                    _Pay.PayValues = SerializeUtil.Serialize(_Pay.PayModel);

                }
            }
            else
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);

            }           
        }
    }
}